package cn.ibizlab.odoo.core.odoo_product.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iproduct_public_category;
import cn.ibizlab.odoo.core.odoo_product.domain.Product_public_category;
import cn.ibizlab.odoo.core.odoo_product.filter.Product_public_categorySearchContext;
import cn.ibizlab.odoo.core.odoo_product.service.IProduct_public_categoryService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[网站产品目录] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Product_public_categoryServiceImpl implements IProduct_public_categoryService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iproduct_public_categoryClientService product_public_categoryClientService;

    @Override
    public Product_public_category get(Integer id) {
        Iproduct_public_category clientModel = product_public_categoryClientService.createModel();
        clientModel.setId(id);
        product_public_categoryClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Product_public_category get(Product_public_category product_public_category) {
        Product_public_category retproduct_public_category = this.get(product_public_category.getId()) ;
    	BeanCopier copier=BeanCopier.create(retproduct_public_category.getClass(), product_public_category.getClass(), false);
        copier.copy(retproduct_public_category, product_public_category, null);
        return product_public_category ;
    }

    @Override
    public void createBatch(List<Product_public_category> product_public_categorys, int batchSize) {

                        //product_public_categoryClientService.createBatch(product_public_categorys,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //product_public_categoryClientService.removeBatch(product_public_categorys) ;

    }

    @Override
    public boolean create(Product_public_category product_public_category) {
        boolean bOk = false ;
        Iproduct_public_category clientModel = this.convert2Model(product_public_category,null);
        product_public_categoryClientService.create(clientModel) ;
        this.convert2Domain(clientModel,product_public_category);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Product_public_category> product_public_categorys, int batchSize) {

        //product_public_categoryClientService.updateBatch(product_public_categorys,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iproduct_public_category clientModel = product_public_categoryClientService.createModel();
        clientModel.setId(id);
        product_public_categoryClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Product_public_category product_public_category) {
        boolean bOk = false ;
        Iproduct_public_category clientModel = this.convert2Model(product_public_category,null);
        product_public_categoryClientService.update(clientModel) ;
        this.convert2Domain(clientModel,product_public_category) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Product_public_category> searchDefault(Product_public_categorySearchContext context) {
        List<Product_public_category> list = new ArrayList<Product_public_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_public_category> clientModelList = product_public_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_public_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_public_category>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据网站产品目录查询
     */
    @Override
    public Page<Product_public_category> selectByOdoo_parent(Integer id){
        Product_public_categorySearchContext context = new Product_public_categorySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_public_category> list = new ArrayList<Product_public_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_public_category> clientModelList = product_public_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_public_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_public_category>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_public_category> selectByOdoo_create(Integer id){
        Product_public_categorySearchContext context = new Product_public_categorySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_public_category> list = new ArrayList<Product_public_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_public_category> clientModelList = product_public_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_public_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_public_category>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_public_category> selectByOdoo_write(Integer id){
        Product_public_categorySearchContext context = new Product_public_categorySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_public_category> list = new ArrayList<Product_public_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_public_category> clientModelList = product_public_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_public_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_public_category>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iproduct_public_category convert2Model(Product_public_category domain , Iproduct_public_category model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = product_public_categoryClientService.createModel() ;
        }

         if(domain.getImage_mediumDirtyFlag())
            model.setImage_medium(domain.getImage_medium());
         if(domain.getImage_smallDirtyFlag())
            model.setImage_small(domain.getImage_small());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getWebsite_meta_titleDirtyFlag())
            model.setWebsite_meta_title(domain.getWebsite_meta_title());
         if(domain.getIs_seo_optimizedDirtyFlag())
            model.setIs_seo_optimized(domain.getIs_seo_optimized());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getWebsite_meta_descriptionDirtyFlag())
            model.setWebsite_meta_description(domain.getWebsite_meta_description());
         if(domain.getWebsite_meta_og_imgDirtyFlag())
            model.setWebsite_meta_og_img(domain.getWebsite_meta_og_img());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWebsite_meta_keywordsDirtyFlag())
            model.setWebsite_meta_keywords(domain.getWebsite_meta_keywords());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getImageDirtyFlag())
            model.setImage(domain.getImage());
         if(domain.getWebsite_idDirtyFlag())
            model.setWebsite_id(domain.getWebsite_id());
         if(domain.getChild_idDirtyFlag())
            model.setChild_id(domain.getChild_id());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getParent_id_textDirtyFlag())
            model.setParent_id_text(domain.getParent_id_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getParent_idDirtyFlag())
            model.setParent_id(domain.getParent_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Product_public_category convert2Domain( Iproduct_public_category model ,Product_public_category domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Product_public_category();
        }

        if(model.getImage_mediumDirtyFlag())
            domain.setImage_medium(model.getImage_medium());
        if(model.getImage_smallDirtyFlag())
            domain.setImage_small(model.getImage_small());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getWebsite_meta_titleDirtyFlag())
            domain.setWebsite_meta_title(model.getWebsite_meta_title());
        if(model.getIs_seo_optimizedDirtyFlag())
            domain.setIs_seo_optimized(model.getIs_seo_optimized());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getWebsite_meta_descriptionDirtyFlag())
            domain.setWebsite_meta_description(model.getWebsite_meta_description());
        if(model.getWebsite_meta_og_imgDirtyFlag())
            domain.setWebsite_meta_og_img(model.getWebsite_meta_og_img());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWebsite_meta_keywordsDirtyFlag())
            domain.setWebsite_meta_keywords(model.getWebsite_meta_keywords());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getImageDirtyFlag())
            domain.setImage(model.getImage());
        if(model.getWebsite_idDirtyFlag())
            domain.setWebsite_id(model.getWebsite_id());
        if(model.getChild_idDirtyFlag())
            domain.setChild_id(model.getChild_id());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getParent_id_textDirtyFlag())
            domain.setParent_id_text(model.getParent_id_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getParent_idDirtyFlag())
            domain.setParent_id(model.getParent_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
